'''
Copyright 2012 Rich Hill

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
'''

from dbfpy import  dbf

class GetForecast():
	def __init__(self,variables):
		
		#print "Reading database:%s" % variables['DatabaseFile']

		database=dbf.Dbf(variables['DatabaseFile'])

		''' Summary of record in database:
		DATE_TIME: 40923.5326389 (<type 'float'>)
		 CHN0_DEG: 20.7 (<type 'float'>)
		  CHN0_RF: 50 (<type 'int'>)
		 CHN0_DEW: 9.9 (<type 'float'>)
		 CHN1_DEG: 3.5 (<type 'float'>)
		  CHN1_RF: 79 (<type 'int'>)
		 CHN1_DEW: 0.2 (<type 'float'>)
		 CHN2_DEG: 0 (<type 'int'>)
		  CHN2_RF: 0 (<type 'int'>)
		 CHN2_DEW: 0 (<type 'int'>)
		 CHN3_DEG: 0 (<type 'int'>)
		  CHN3_RF: 0 (<type 'int'>)
		 CHN3_DEW: 0 (<type 'int'>)
		 CHN4_DEG: 0 (<type 'int'>)
		  CHN4_RF: 0 (<type 'int'>)
		 CHN4_DEW: 0 (<type 'int'>)
		 CHN5_DEG: 0 (<type 'int'>)
		  CHN5_RF: 0 (<type 'int'>)
		 CHN5_DEW: 0 (<type 'int'>)
		 PRES_LOC: 1007.6241 (<type 'float'>)
		 PRES_ABS: 1023.5 (<type 'float'>)
		 FORECAST: 4 (<type 'int'>)
		  W_CHILL: 0 (<type 'int'>)
		   W_TEMP: 0 (<type 'int'>)
		   W_GUST: 0 (<type 'int'>)
		  W_SPEED: 0 (<type 'int'>)
		DIRECTION: 0 (<type 'int'>)
		 RAIN_SUM: 169.8 (<type 'float'>)
		 ERR_CODE: 1.023456 (<type 'float'>)
		'''
		self.lastrecord=database[len(database)-1]
		self.penultimaterecord=database[len(database)-2]

		self.currentforecast=variables["ForecastDay"][self.lastrecord['FORECAST'] ]
		self.penultimateforecast=variables["ForecastDay"][self.penultimaterecord['FORECAST'] ]
		
		self.dbsize=len(database)
		
		database.close()
		
class GetRainfall():
	days=''
	periodrainfall=0
	def __init__(self,variables,days):
		#print "Reading database %s to get rainfall figures" % variables['DatabaseFile']
		
		# read backwards through database until we have all of the specified time frames records
		database=dbf.Dbf(variables['DatabaseFile'])
		today=int(str(database[len(database)-1]['DATE_TIME']).split('.')[0])
		periodrainfalllist=[]
		
		for record in range(len(database)-1,0,-1):
			recordday=int(str(database[record]['DATE_TIME']).split('.')[0])
			if int(today) - int(recordday)==days: break
			periodrainfalllist.append(database[record]['RAIN_SUM'])
		
		periodrainfalllist.reverse() # we need to reverse the list of rain fall as we appended to it backwards
		self.periodrainfall=periodrainfalllist[len(periodrainfalllist)-1]-periodrainfalllist[0]
		
			
			



		